package game;

import java.awt.Color;
import java.awt.Cursor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

/**
 * @author Chris
 */
public class MainMenu extends JPanel {

    private static final String SINGLEPLAYER = "Un jugador";
    private static final String MULTIPLAYER = "Dos jugadores _m/";
    private static final String RANKING = "Ver ranking";
    private static final String EXIT = "Salir del juego";
    private Game game;

    public MainMenu(Game game) {
        this.game = game;
        createMenu();
    }

    private void createMenu() {
        JButton singleplayer = new JButton(MainMenu.SINGLEPLAYER),
                multiplayer = new JButton(MainMenu.MULTIPLAYER),
                ranking = new JButton(MainMenu.RANKING),
                gohome = new JButton(MainMenu.EXIT);

        setLayout(null);
        setDoubleBuffered(false);
        setBackground(Color.DARK_GRAY);


        singleplayer.setBounds((Game.FWIDTH - 300) / 2, 100, 300, 70);
        singleplayer.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        game.singleplayer_handler(askPlayerName());
                    }
                });
        singleplayer.setCursor(new Cursor(Cursor.HAND_CURSOR));
        add(singleplayer);


        multiplayer.setBounds((Game.FWIDTH - 300) / 2, 220, 300, 70);
        multiplayer.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        game.multiplayer_handler(askPlayerName());
                    }
                });
        multiplayer.setCursor(new Cursor(Cursor.HAND_CURSOR));
        add(multiplayer);


        ranking.setBounds((Game.FWIDTH - 300) / 2, 340, 300, 70);
        ranking.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        game.ranking_handler();
                    }
                });
        add(ranking);


        gohome.setBounds((Game.FWIDTH - 300) / 2, 460, 300, 70);
        gohome.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        System.exit(0);
                    }
                });
        add(gohome);
    }

    private String askPlayerName() {
        String pname = (String) JOptionPane.showInputDialog(
                this.game,
                "Ingrese su nombre: ",
                "Nombre del jugador",
                JOptionPane.QUESTION_MESSAGE,
                null, null,
                "Player");

        if (pname != null) {
            if (pname.trim().isEmpty()) {
                pname = "Player";
            }
            return pname;
        }
        return null;
    }
}
